package com.spi.soconversion.base.handlers;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.DBConnection;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.QueryConstants;
import com.spi.soconversion.util.QueryReader;
import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCFileWriter;
import com.spi.soconversion.util.SOCUtility;

public class InstallBaseConversionErrorHandler {

	private String d_logFileName;
	private SOCFileWriter d_fileWriter;	
	private ArrayList<ConversionError> errorList = new ArrayList<ConversionError>();
	
	public InstallBaseConversionErrorHandler(String logFileName, SOCFileWriter fileWriter)
	{
		d_logFileName = logFileName;
		d_fileWriter = fileWriter;
	}
	
	public void addErrors(long instanceId, String error, int batchNumer) throws SOCException
	{
		ConversionError sce = new ConversionError(instanceId,batchNumer,error);
		
		if(errorList!=null && errorList.size()==SOCConstants.CONVERSION_ERROR_UPDATE_FREQUENCY)
		{
			updateErrors();
			errorList = new ArrayList<ConversionError>();
			errorList.add(sce);
		}
		else
		{
			errorList.add(sce);
		}
	}
	
	public void updateErrors() throws SOCException
	{
		boolean hasErrors= false;
		Iterator<ConversionError> iter=errorList.iterator();
		if(iter.hasNext())
	    {
	    	d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_PROCESSING_INSTALL_BASE_ERROR_STARTS));
	    	hasErrors=true;
	    }
		if(hasErrors)
		{
			String insertInstallBaseErrorsQuery = QueryReader.getQuery(QueryConstants.INSERT_INSTALLBASE_CONVERSION_ERRORS);
		
			PreparedStatement installBaseErrorsStmt = null;
			try {
				DBConnection.getConnection().setAutoCommit(false);
				installBaseErrorsStmt = DBConnection.getConnection().prepareStatement(insertInstallBaseErrorsQuery);
			} catch (SQLException se) {
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.FAILED_CREATE_STMT_INSTALLBASE_CONVERSION_ERRORS)+SOCConstants.NEW_LINE+se.getMessage()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.FAILED_CREATE_STMT_INSTALLBASE_CONVERSION_ERRORS),se);
			}	   
		   
		   
			while(iter.hasNext())
	    	{	
				ConversionError  sce = iter.next();
				try {
					installBaseErrorsStmt.setLong(1,sce.getInstanceId());
					installBaseErrorsStmt.setInt(2,sce.getBatchNum());
					installBaseErrorsStmt.setString(3,sce.getError());
					installBaseErrorsStmt.addBatch();
				} catch (SQLException se) {
					d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
							ErrorConstants.FAILED_ADDING_BATCH_INSTALLBASE_CONVERSION_ERRORS)+SOCConstants.NEW_LINE+se.getMessage()));
					throw new SOCException(
							ErrorMessageReader.getError(
									ErrorConstants.FAILED_ADDING_BATCH_INSTALLBASE_CONVERSION_ERRORS),se);
				}
				
	    	}
		    try {
				installBaseErrorsStmt.executeBatch();
				DBConnection.getConnection().commit();
				DBConnection.getConnection().setAutoCommit(true);
				 d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_PROCESSING_INSTALL_BASE_ERROR_ENDS));
			} catch (SQLException se) {
				d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.FAILED_UPDATE_BATCH_INSTALLBASE_CONVERSION_ERRORS)+SOCConstants.NEW_LINE+se.getMessage()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.FAILED_UPDATE_BATCH_INSTALLBASE_CONVERSION_ERRORS),se);
			}
		}
	}
}
